//
//  YLAMyViewController.swift
//  Your Lover AI
//
//  Created by 阿忠 on 2024/6/18.
//

import Foundation
import UIKit
import PopupDialog

class YLAMyViewController: UIViewController, UITableViewDataSource,UITableViewDelegate, YLAMYOthersCell.CellDelegate{
 
    
    
    var title_lb: UILabel!      // 标题
    var tableView : UITableView!
    var nameView: YLANameView! = nil
    var blurEffectView: UIVisualEffectView! = nil
    var ratingView: YLARatingView! = nil
    private let AncientBookCellID = "YLAChatCell"
    
    lazy var bgImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "bg"))
        return image
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.red
 
        setUI()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController!.tabBar.isHidden = false
    }
    
    func setUI() {
        
        view.addSubview(bgImg)
        bgImg.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
        
        title_lb = UILabel()
        title_lb.textColor = .white
        title_lb.font = UIFont.boldSystemFont(ofSize: 25)
        title_lb.textAlignment = .left
        title_lb.text = "Me"
        view.addSubview(title_lb)
        title_lb.snp.makeConstraints { make in
            make.left.equalTo(17)
            make.top.equalTo(55)
            make.height.equalTo(25)
        }
        
        tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(title_lb.snp.bottom).offset(20)
            make.bottom.equalTo(-85)
        }
        view.addSubview(tableView)
        tableView.dataSource = self
        tableView.delegate = self
        extendedLayoutIncludesOpaqueBars = true;
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false;
        };
        
        tableView.reloadData()
        
    }
    
    //MARK: UITableViewDataSource
    // cell的个数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    // UITableViewCell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            let cellid = "testCellID"
            var cell:YLAMYVIPCell? = tableView.dequeueReusableCell(withIdentifier: cellid) as? YLAMYVIPCell
            if cell==nil {
                cell = YLAMYVIPCell(style:.subtitle, reuseIdentifier: cellid)
            }
            cell?.backgroundColor = .clear
            cell?.selectionStyle = .none
            
            return cell!
        }else if indexPath.row == 1 {
            let cellid = "testCellID"
            var cell:YLAMYNameCell? = tableView.dequeueReusableCell(withIdentifier: cellid) as? YLAMYNameCell
            if cell==nil {
                cell = YLAMYNameCell(style:.subtitle, reuseIdentifier: cellid)
            }
            cell?.backgroundColor = .clear
            cell?.selectionStyle = .none
            
            return cell!
        }else if indexPath.row == 2 {
            let cellid = "testCellID"
            var cell:YLAMYGendderCell? = tableView.dequeueReusableCell(withIdentifier: cellid) as? YLAMYGendderCell
            if cell==nil {
                cell = YLAMYGendderCell(style:.subtitle, reuseIdentifier: cellid)
            }
            cell?.backgroundColor = .clear
            cell?.selectionStyle = .none
            
            return cell!
        }else if indexPath.row == 3 {
            let cellid = "testCellID"
            var cell:YLAMYOthersCell? = tableView.dequeueReusableCell(withIdentifier: cellid) as? YLAMYOthersCell
            if cell==nil {
                cell = YLAMYOthersCell(style:.subtitle, reuseIdentifier: cellid)
            }
            cell?.delegate = self
            cell?.backgroundColor = .clear
            cell?.selectionStyle = .none
            
            return cell!
        }
        else{
            let cellid = "testCellID"
            var cell:YLAMYNameCell? = tableView.dequeueReusableCell(withIdentifier: cellid) as? YLAMYNameCell
            if cell==nil {
                cell = YLAMYNameCell(style:.subtitle, reuseIdentifier: cellid)
            }
            cell?.backgroundColor = .clear
            cell?.selectionStyle = .none
            
            return cell!
        }
       
    }
    
    //MARK: UITableViewDelegate
    // 设置cell高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.row == 0 {
            return 93
        }
        if indexPath.row == 1 {
            return 107
        }
        if indexPath.row == 2 {
            return 180
        }
        if indexPath.row == 3 {
            return 286
        }
        return 95
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    // 选中cell后执行此方法
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
      print("------选中了chat的cell------")
        if indexPath.row == 0 {
        let storeVc = YLAStroeViewControll()
            self.navigationController?.pushViewController(storeVc, animated: true)
        }else if indexPath.row == 1 {
            self.tabBarController!.tabBar.isHidden = true
            let blurEffect = UIBlurEffect(style: .light) // 可调整模糊效果的样式
            self.blurEffectView = UIVisualEffectView(effect: blurEffect)
            self.blurEffectView.frame = self.view.bounds
            self.view.addSubview(self.blurEffectView)
           
            let dialogFrame = CGRect(x: 0, y: 200, width: 270, height: 200)
            self.nameView = YLANameView(frame: dialogFrame, title: "续费", paymentMethod: "支付宝", paymentDescription: "请使用支付宝完成支付。")
            
            self.view.addSubview(self.nameView)
            self.nameView.snp.makeConstraints { make in
                make.centerY.centerY.equalToSuperview()
                make.left.equalTo(43)
                make.right.equalTo(-43)
                make.height.equalTo(165)
            }
            
            self.view.insertSubview(self.blurEffectView, belowSubview: self.nameView)
            
            self.nameView.onCancel = {
                // 执行取消操作
                self.tabBarController!.tabBar.isHidden = false
                self.nameView.removeFromSuperview()
                self.blurEffectView.removeFromSuperview()
            }

            nameView.onConfirm = { text in
                if let password = text {
                    let  user_name = password
                    let userDefault = UserDefaults.standard
                    userDefault.set(user_name, forKey: "user_name")
                    self.tabBarController!.tabBar.isHidden = false
                    self.nameView.removeFromSuperview()
                    self.blurEffectView.removeFromSuperview()
                    tableView.reloadData()
                } else {
                    print("取消了输入")
                }
            }
        }else{
            
        }
   
    }
    
    func cell(_ cell: YLAMYOthersCell, toher1: Any) {
        print("-----------回调到外面的1--------------")
        let webvc = YLAWebViewController()
        webvc.urlWeb = "https://yourloverai.xyz/yourloverai-privacy.html"
        webvc.titleWeb = "Privacy Policy"
        self.navigationController?.pushViewController(webvc, animated: true)
    }
    
    func cell(_ cell: YLAMYOthersCell, toher2: Any) {
        print("-----------回调到外面的2--------------")
        let webvc = YLAWebViewController()
        webvc.urlWeb = "https://www.yourloverai.xyz/yourloverai-terms.html"
        webvc.titleWeb = "Term"
        self.navigationController?.pushViewController(webvc, animated: true)
    }
    
    func cell(_ cell: YLAMYOthersCell, toher3: Any) {
        print("-----------回调到外面的3--------------")
        let popup = PopupDialog(title: "Notice", message: "Please go to Settings - Subscribe to cancel", image: nil)
        self.present(popup, animated: true, completion: nil)
    }
    
    func cell(_ cell: YLAMYOthersCell, toher4: Any) {
        print("-----------回调到外面的4--------------")
        
        self.tabBarController!.tabBar.isHidden = true
        let blurEffect = UIBlurEffect(style: .light) // 可调整模糊效果的样式
        self.blurEffectView = UIVisualEffectView(effect: blurEffect)
        self.blurEffectView.frame = self.view.bounds
        self.view.addSubview(self.blurEffectView)
       
        let dialogFrame = CGRect(x: 0, y: 200, width: 270, height: 200)
        self.ratingView = YLARatingView(frame: dialogFrame, title: "续费", paymentMethod: "支付宝", paymentDescription: "请使用支付宝完成支付。")
        
        self.view.addSubview(self.ratingView)
        self.ratingView.snp.makeConstraints { make in
            make.centerY.centerY.equalToSuperview()
            make.left.equalTo(43)
            make.right.equalTo(-43)
            make.height.equalTo(256)
        }
        
        self.view.insertSubview(self.blurEffectView, belowSubview: self.ratingView)
        
        self.ratingView.onCancel = {
            // 执行取消操作
            self.tabBarController!.tabBar.isHidden = false
            self.ratingView.removeFromSuperview()
            self.blurEffectView.removeFromSuperview()
        }

        ratingView.onConfirm = { [self] text in
            if let startInt = text {
                self.tabBarController!.tabBar.isHidden = false
                self.ratingView.removeFromSuperview()
                self.blurEffectView.removeFromSuperview()
                tableView.reloadData()
                if startInt <= 4 {
                    //弹窗提示
                    let popup = PopupDialog(title: "Notice", message: "thank you for your rating", image: nil)
                    self.present(popup, animated: true, completion: nil)
                }else{
                    //跳转到appstore评分
                    let userDefaults = UserDefaults.standard
                    userDefaults.set(true, forKey: "isUserEvaluate")
                    if let url = URL(string: "itms-apps://itunes.apple.com/app/id<6504508706>?action=write-review") {
                        if UIApplication.shared.canOpenURL(url) {
                            UIApplication.shared.open(url, options: [:], completionHandler: nil)
                        }
                    }
                }
            } else {
                print("取消了输入")
            }
        }
        
    }
    
    
}
